# Ask CMake to perform builds in a temporary directory for all of these.
# We also use EXCLUDE_FROM_ALL to ensure we only build and install what we want
#ZLIB
get_filename_component(ZLIB_ROOT "zlib" REALPATH BASE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(ZLIB_ROOT "${ZLIB_ROOT}" CACHE STRING "ZLib root location" FORCE)
advanced_option(ZLIB_BUILD_EXAMPLES "Enable Zlib Examples" OFF)
mark_as_advanced(ZLIB_ROOT)
# Png depends on zlib
add_subdirectory(zlib ${TORQUE_LIB_TARG_DIRECTORY}/zlib EXCLUDE_FROM_ALL)

if(APPLE)
    enable_language(OBJC)
    advanced_option(AL_ALEXT_PROTOTYPES "Use Extended OpenAL options" OFF)
else()
    advanced_option(AL_ALEXT_PROTOTYPES "Use Extended OpenAL options" ON)
endif()

if(AL_ALEXT_PROTOTYPES)
	addDef( "AL_ALEXT_PROTOTYPES" )
endif()

#SDL
set(SDL_SHARED ON CACHE BOOL "Build a shared version of the library" FORCE)
set(SDL_STATIC OFF CACHE BOOL "Build a static version of the library" FORCE)
mark_as_advanced(SDL2MAIN_LIBRARY)
mark_as_advanced(SDL2_CORE_LIBRARY)
mark_as_advanced(SDL2_INCLUDE_DIR)
mark_as_advanced(SDL2_DISABLE_INSTALL)
mark_as_advanced(SDL2_DISABLE_SDL2MAIN)
mark_as_advanced(SDL2_DISABLE_UNINSTALL)
mark_as_advanced(SDL_3DNOW)
mark_as_advanced(SDL_ALSA)
mark_as_advanced(SDL_ALTIVEC)
mark_as_advanced(SDL_ARMNEON)
mark_as_advanced(SDL_ARMSIMD)
mark_as_advanced(SDL_ARTS)
mark_as_advanced(SDL_ASAN)
mark_as_advanced(SDL_ASSEMBLY)
mark_as_advanced(SDL_ASSERTIONS)
mark_as_advanced(SDL_ATOMIC)
mark_as_advanced(SDL_AUDIO)
mark_as_advanced(SDL_BACKGROUNDING_SIGNAL)
mark_as_advanced(SDL_CCACHE)
mark_as_advanced(SDL_CLOCK_GETTIME)
mark_as_advanced(SDL_COCOA)
mark_as_advanced(SDL_CPUINFO)
mark_as_advanced(SDL_DIRECTFB)
mark_as_advanced(SDL_DIRECTX)
mark_as_advanced(SDL_DISKAUDIO)
mark_as_advanced(SDL_DUMMYAUDIO)
mark_as_advanced(SDL_DUMMYVIDEO)
mark_as_advanced(SDL_ESD)
mark_as_advanced(SDL_EVENTS)
mark_as_advanced(SDL_FILE)
mark_as_advanced(SDL_FILESYSTEM)
mark_as_advanced(SDL_FORCE_STATIC_VCRT)
mark_as_advanced(SDL_FOREGROUNDING_SIGNAL)
mark_as_advanced(SDL_FUSIONSOUND)
mark_as_advanced(SDL_GCC_ATOMICS)
mark_as_advanced(SDL_HAPTIC)
mark_as_advanced(SDL_HIDAPI)
mark_as_advanced(SDL_HIDAPI_JOYSTICK)
mark_as_advanced(SDL_HIDAPI_LIBUSB)
mark_as_advanced(SDL_INSTALL_TESTS)
mark_as_advanced(SDL_JACK)
mark_as_advanced(SDL_JOYSTICK)
mark_as_advanced(SDL_KMSDRM)
mark_as_advanced(SDL_LIBC)
mark_as_advanced(SDL_LIBSAMPLERATE)
mark_as_advanced(SDL_LIBUDEV)
mark_as_advanced(SDL_LOADSO)
mark_as_advanced(SDL_LOCALE)
mark_as_advanced(SDL_METAL)
mark_as_advanced(SDL_MISC)
mark_as_advanced(SDL_MMX)
mark_as_advanced(SDL_NAS)
mark_as_advanced(SDL_OFFSCREEN)
mark_as_advanced(SDL_OPENGL)
mark_as_advanced(SDL_OPENGLES)
mark_as_advanced(SDL_PIPEWIRE)
mark_as_advanced(SDL_POWER)
mark_as_advanced(SDL_PTHREADS)
mark_as_advanced(SDL_PULSEAUDIO)
mark_as_advanced(SDL_RENDER)
mark_as_advanced(SDL_RENDER_D3D)
mark_as_advanced(SDL_RENDER_METAL)
mark_as_advanced(SDL_RPATH)
mark_as_advanced(SDL_RPI)
mark_as_advanced(SDL_SENSOR)
mark_as_advanced(SDL_SHARED)
mark_as_advanced(SDL_SNDIO)
mark_as_advanced(SDL_SSE)
mark_as_advanced(SDL_SSE2)
mark_as_advanced(SDL_SSE3)
mark_as_advanced(SDL_SSEMATH)
mark_as_advanced(SDL_STATIC)
mark_as_advanced(SDL_SYSTEM_ICONV)
mark_as_advanced(SDL_TEST)
mark_as_advanced(SDL_TESTS)
mark_as_advanced(SDL_THREADS)
mark_as_advanced(SDL_TIMERS)
mark_as_advanced(SDL_VENDOR_INFO)
mark_as_advanced(SDL_VIDEO)
mark_as_advanced(SDL_VIRTUAL_JOYSTICK)
mark_as_advanced(SDL_VIVANTE)
mark_as_advanced(SDL_VULKAN)
mark_as_advanced(SDL_WASAPI)
mark_as_advanced(SDL_WAYLAND)
mark_as_advanced(SDL_WERROR)
mark_as_advanced(SDL_X11)
mark_as_advanced(SDL_XINPUT)
mark_as_advanced(SDL2_DIR)
add_subdirectory(sdl ${TORQUE_LIB_TARG_DIRECTORY}/sdl2 EXCLUDE_FROM_ALL)

add_subdirectory(nativeFileDialogs ${TORQUE_LIB_TARG_DIRECTORY}/nfd EXCLUDE_FROM_ALL)
add_subdirectory(convexMath ${TORQUE_LIB_TARG_DIRECTORY}/convexMath EXCLUDE_FROM_ALL)

# Assimp
advanced_option(ASSIMP_HUNTER_ENABLED "Enable Hunter package manager support" OFF)
advanced_option(ASSIMP_BUILD_NO_OWN_ZLIB "" ON)
set(BUILD_SHARED_LIBS OFF )
advanced_option(ASSIMP_BUILD_FRAMEWORK "Build package as Mac OS X Framework bundle." OFF )
advanced_option(ASSIMP_DOUBLE_PRECISION "Set to ON to enable double precision processing" OFF )
advanced_option(ASSIMP_OPT_BUILD_PACKAGES "Set to ON to generate CPack configuration files and packaging targets" OFF )
advanced_option(ASSIMP_ANDROID_JNIIOSYSTEM "Android JNI IOSystem support is active" OFF )
advanced_option(ASSIMP_NO_EXPORT "Disable Assimp's export functionality." OFF )
advanced_option(ASSIMP_BUILD_ASSIMP_TOOLS "If the supplementary tools for Assimp are built in addition to the library." OFF )
advanced_option(ASSIMP_BUILD_SAMPLES "If the official samples are built as well (needs Glut)." OFF )
advanced_option(ASSIMP_BUILD_TESTS "If the test suite for Assimp is built in addition to the library." OFF )
advanced_option(ASSIMP_COVERALLS "Enable this to measure test coverage." OFF )
advanced_option(ASSIMP_INSTALL "Disable this if you want to use assimp as a submodule." OFF )
advanced_option(ASSIMP_WARNINGS_AS_ERRORS "Treat all warnings as errors." OFF )
advanced_option(ASSIMP_ASAN "Enable AddressSanitizer." OFF )
advanced_option(ASSIMP_UBSAN "Enable Undefined Behavior sanitizer." OFF )
advanced_option(ASSIMP_BUILD_DOCS "Build documentation using Doxygen." OFF )
advanced_option(ASSIMP_INJECT_DEBUG_POSTFIX "Inject debug postfix in .a/.so/.dll lib names" ON )
advanced_option(ASSIMP_IGNORE_GIT_HASH "Don't call git to get the hash." ON )
advanced_option(ASSIMP_BUILD_ZLIB "Build your own zlib" OFF )
advanced_option(ASSIMP_ANDROID_JNIIOSYSTEM "Android JNI IOSystem support is active" ON)
advanced_option(ASSIMP_BUILD_DRACO "If the Draco libraries are to be built. Primarily for glTF" OFF )
advanced_option(ASSIMP_BUILD_NONFREE_C4D_IMPORTER "Build the C4D importer, which relies on the non-free Cineware SDK." OFF)
IF(MSVC)
    advanced_option( ASSIMP_INSTALL_PDB "Install MSVC debug files." ON )
endif()
advanced_option(ASSIMP_RAPIDJSON_NO_MEMBER_ITERATOR "Suppress rapidjson warning on MSVC (NOTE: breaks android build)" ON )
advanced_option(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT "default value of all ASSIMP_BUILD_XXX_IMPORTER values" ON)
advanced_option(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT "default value of all ASSIMP_BUILD_XXX_EXPORTER values" ON)
mark_as_advanced(ASSIMP_ARCHIVE_OUTPUT_DIRECTORY)
mark_as_advanced(ASSIMP_BIN_INSTALL_DIR)
mark_as_advanced(ASSIMP_LIB_INSTALL_DIR)
mark_as_advanced(ASSIMP_INCLUDE_INSTALL_DIR)
mark_as_advanced(ASSIMP_LIBRARY_OUTPUT_DIRECTORY)
mark_as_advanced(ASSIMP_LIBRARY_SUFFIX)
mark_as_advanced(ASSIMP_PACKAGE_VERSION)
mark_as_advanced(ASSIMP_RUNTIME_OUTPUT_DIRECTORY)
add_subdirectory(assimp ${TORQUE_LIB_TARG_DIRECTORY}/assimp EXCLUDE_FROM_ALL)
target_compile_definitions(assimp PUBLIC ASSIMP_BUILD_NO_OWN_ZLIB)

if (TORQUE_CPU_ARM32 OR TORQUE_CPU_ARM64)
    if(NOT APPLE)
	    set(PNG_ARM_NEON on CACHE BOOL "" FORCE)
    endif(NOT APPLE)
endif (TORQUE_CPU_ARM32 OR TORQUE_CPU_ARM64)

if(APPLE)
    set(PNG_ARM_NEON off CACHE BOOL "" FORCE)
endif()

#PNG STB Update: Not sure if these are still needed.
set(PNG_STATIC on CACHE BOOL "" FORCE)
mark_as_advanced(PNG_STATIC)
set(PNG_SHARED off CACHE BOOL "" FORCE)
mark_as_advanced(PNG_SHARED)
set(PNG_BUILD_ZLIB on CACHE BOOL "" FORCE)
mark_as_advanced(PNG_BUILD_ZLIB)
set(PNG_TESTS off CACHE BOOL "" FORCE)
mark_as_advanced(PNG_TESTS)
set(PNG_HARDWARE_OPTIMIZATIONS on CACHE BOOL "" FORCE)
mark_as_advanced(PNG_HARDWARE_OPTIMIZATIONS)

if(APPLE)
    set(PNG_FRAMEWORK on CACHE BOOL "" FORCE)
    addDef(PNG_DEBUG Debug)
endif()

mark_as_advanced(PNG_DEBUG)
mark_as_advanced(PNG_FRAMEWORK)
mark_as_advanced(PNG_PREFIX)

add_subdirectory(tinyxml2 ${TORQUE_LIB_TARG_DIRECTORY}/tinyxml2 EXCLUDE_FROM_ALL)
add_subdirectory(opcode ${TORQUE_LIB_TARG_DIRECTORY}/opcode EXCLUDE_FROM_ALL)
add_subdirectory(pcre ${TORQUE_LIB_TARG_DIRECTORY}/pcre EXCLUDE_FROM_ALL)

add_subdirectory(squish ${TORQUE_LIB_TARG_DIRECTORY}/squish EXCLUDE_FROM_ALL)
add_subdirectory(collada ${TORQUE_LIB_TARG_DIRECTORY}/collada EXCLUDE_FROM_ALL)
add_subdirectory(glad ${TORQUE_LIB_TARG_DIRECTORY}/glad EXCLUDE_FROM_ALL)

#hidden options
if(TORQUE_SFX_OPENAL)
    advanced_option(TORQUE_OGGVORBIS "Enable OGG Vorbis" ON)
    advanced_option(ALSOFT_EAX "Enable legacy EAX extensions" ${WIN32})
    advanced_option(ALSOFT_UPDATE_BUILD_VERSION "Update git build version info" ON)
    mark_as_advanced(ALSOFT_BACKEND_COREAUDIO)
    advanced_option(ALSOFT_BACKEND_DSOUND "Enable DirectSound backend" OFF)    
    advanced_option(ALSOFT_BACKEND_JACK "Enable Jack backend" OFF)
    advanced_option(ALSOFT_BACKEND_OBOE "Enable OBOE backend" OFF)
    advanced_option(ALSOFT_BACKEND_OPENSL "Enable OpenSL backend" OFF)
    advanced_option(ALSOFT_BACKEND_PIPEWIRE "Enable PipeWire backend" OFF)
    advanced_option(ALSOFT_BACKEND_PORTAUDIO "Enable PortAudio backend" OFF)
    advanced_option(ALSOFT_BACKEND_PULSEAUDIO "Enable PulseAudio backend" OFF)
    advanced_option(ALSOFT_BACKEND_SDL2 "Use SDL2 Backend" ON)
    advanced_option(ALSOFT_BACKEND_WASAPI "Enable WASAPI backend" OFF)
    advanced_option(ALSOFT_BACKEND_WAVE "Enable Wave backend" OFF)
    advanced_option(ALSOFT_BACKEND_WINMM "Enable WinMM backend" OFF)
    mark_as_advanced(ALSOFT_BUILD_ROUTER)
    mark_as_advanced(ALSOFT_CPUEXT_NEON)
    mark_as_advanced(ALSOFT_CPUEXT_SSE)
    mark_as_advanced(ALSOFT_CPUEXT_SSE2)
    mark_as_advanced(ALSOFT_CPUEXT_SSE3)
    mark_as_advanced(ALSOFT_CPUEXT_SSE4_1)
    mark_as_advanced(ALSOFT_DLOPEN)
    mark_as_advanced(ALSOFT_EAX)
    advanced_option(ALSOFT_EMBED_HRTF_DATA "Embed the HRTF data files (increases library footprint)" OFF)
    advanced_option(ALSOFT_EXAMPLES  "Build example programs"  OFF)
    advanced_option(ALSOFT_INSTALL "Install OpenAL" OFF) # we move it ourselves
    advanced_option(ALSOFT_INSTALL_CONFIG "Install alsoft.conf sample configuration file" OFF)
    advanced_option(ALSOFT_INSTALL_HRTF_DATA "Install HRTF data files" OFF)
    advanced_option(ALSOFT_INSTALL_AMBDEC_PRESETS "Install AmbDec preset files" OFF)
    advanced_option(ALSOFT_INSTALL_EXAMPLES "Install example programs (alplay, alstream, ...)" OFF)
    advanced_option(ALSOFT_INSTALL_UTILS "Install utility programs (openal-info, alsoft-config, ...)" OFF)
    mark_as_advanced(ALSOFT_UPDATE_BUILD_VERSION)
    mark_as_advanced(ALSOFT_NO_CONFIG_UTIL)
    mark_as_advanced(ALSOFT_REQUIRE_COREAUDIO)
    mark_as_advanced(ALSOFT_REQUIRE_DSOUND)
    mark_as_advanced(ALSOFT_REQUIRE_JACK)
    mark_as_advanced(ALSOFT_REQUIRE_NEON)
    mark_as_advanced(ALSOFT_REQUIRE_OBOE)
    mark_as_advanced(ALSOFT_REQUIRE_OPENSL)
    mark_as_advanced(ALSOFT_REQUIRE_PIPEWIRE)
    mark_as_advanced(ALSOFT_REQUIRE_PORTAUDIO)
    mark_as_advanced(ALSOFT_REQUIRE_PULSEAUDIO)
    mark_as_advanced(ALSOFT_REQUIRE_SDL2)
    mark_as_advanced(ALSOFT_REQUIRE_SSE)
    mark_as_advanced(ALSOFT_REQUIRE_SSE2)
    mark_as_advanced(ALSOFT_REQUIRE_SSE3)
    mark_as_advanced(ALSOFT_REQUIRE_SSE4_1)
    mark_as_advanced(ALSOFT_REQUIRE_WASAPI)
    mark_as_advanced(ALSOFT_REQUIRE_WINMM)
    mark_as_advanced(ALSOFT_SEARCH_INSTALL_DATADIR)
    mark_as_advanced(ALSOFT_TESTS)
    mark_as_advanced(ALSOFT_UTILS)
    mark_as_advanced(ALSOFT_WERROR)
endif()

set(BUILD_SHARED_LIBS OFF )
if(TORQUE_TESTING)
    add_subdirectory(gtest ${TORQUE_LIB_TARG_DIRECTORY}/gtest EXCLUDE_FROM_ALL)
endif()
#endif()

#misc randoms
mark_as_advanced(WINDRES)
mark_as_advanced(AUDIOUNIT_INCLUDE_DIR)
mark_as_advanced(CCACHE_BINARY)
mark_as_advanced(COREAUDIO_FRAMEWORK)
mark_as_advanced(FORCE_STATIC_VCRT)
mark_as_advanced(HAVE_GIT)
mark_as_advanced(LIBRARY_SUFFIX)
mark_as_advanced(USE_STATIC_CRT)

mark_as_advanced(BUILD_CXXLIBS)
mark_as_advanced(BUILD_DOCS)
mark_as_advanced(BUILD_REGTEST)
mark_as_advanced(BUILD_UTILS)

mark_as_advanced(ENABLE_64_BIT_WORDS)
mark_as_advanced(ENABLE_BOW_DOCS)
mark_as_advanced(ENABLE_EXTERNAL_LIBS)

mark_as_advanced(WITH_ASM)
mark_as_advanced(WITH_AVX)
mark_as_advanced(WITH_FORTIFY_SOURCE)
mark_as_advanced(WITH_OGG)
mark_as_advanced(WITH_STACK_PROTECTOR)
